//
//  KSRefundDetailViewController.swift
//  Supermarket
//
//  Created by liuxm on 2017/9/30.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

/// 退款详情
class KSRefundDetailViewController: KSBaseViewController {

    @IBOutlet weak var tableView: UITableView!
    var refundNumber: String = ""
    private var model: KSOrderRefundDetailModel?
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = "退款详情"
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        let statusNib = UINib(nibName: "KSStatusTableViewCell", bundle: nil)
        self.tableView.register(statusNib, forCellReuseIdentifier: "KSStatusTableViewCell")
        
        let historyNib = UINib(nibName: "KSTitleSkipTableViewCell", bundle: nil)
        self.tableView.register(historyNib, forCellReuseIdentifier: "KSTitleSkipTableViewCell")
        
        let detailNib = UINib(nibName: "KSRefundDetailTableViewCell", bundle: nil)
        self.tableView.register(detailNib, forCellReuseIdentifier: "KSRefundDetailTableViewCell")
        
        let detailInfoNib = UINib(nibName: "KSRefundDetailInfoTableViewCell", bundle: nil)
        self.tableView.register(detailInfoNib, forCellReuseIdentifier: "KSRefundDetailInfoTableViewCell")
        self.requestData()
    }
    
    private func requestData() {
        self.request.requestOrderRefundDetail(selectType: "2", refundNumber: "01920171010131236361")
        tableView.reloadData()
    }

    override func requestSuccess(result: String, apiType: KSApiType) {
        super.requestSuccess(result: result, apiType: apiType)
        self.model = KSOrderRefundDetailModel.deserialize(from: result)
        tableView.reloadData()
    }
    
    @IBAction func logisticDetailEvent(_ sender: Any) {
        let vc = KSCheckLogisticsViewController()
        vc.isConsult = false
        vc.num = (model?.data?.express_number)!
        vc.logisticId = "\(Int((model?.data?.kuaidi_id)!))"
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension KSRefundDetailViewController: UITableViewDelegate, UITableViewDataSource {
    
    // UITableViewDataSource
    
    func numberOfSections(in tableView: UITableView) -> Int {
        var num = 0
        if model != nil && (model?.data?.refund_goods?.count)! > 0 {
            num = 3 + (model?.data?.refund_goods?.count)!
        }
        return num
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    // UITableViewDelegate
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        var height = 175
        if indexPath.section == 0 {
            height = 90
        } else if indexPath.section == 1 {
            height = 50
        } else if indexPath.section == 3 {
            height = 316
        }
        
        return CGFloat(height)
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "KSStatusTableViewCell", for: indexPath) as! KSStatusTableViewCell
            cell.statusLabel.text = "你已成功申请退款，商家审核中，请耐心等待"
            
            return cell
        } else if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "KSTitleSkipTableViewCell", for: indexPath) as! KSTitleSkipTableViewCell
            cell.titleLabel.text = "协商历史"
            return cell
        } else if indexPath.section == 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "KSRefundDetailInfoTableViewCell", for: indexPath) as! KSRefundDetailInfoTableViewCell
            cell.setDataWithModel(model: (model?.data)!)
            return cell
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "KSRefundDetailTableViewCell", for: indexPath) as! KSRefundDetailTableViewCell
        cell.setDataWithModel(model: (model?.data?.refund_goods)![indexPath.section - 2])
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // 协商历史
        if indexPath.section == 1 {
            let vc = KSCheckLogisticsViewController()
            vc.consultArray = (model?.data?.refund_operation_logs)!
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
}
